package com.liang.blog.controller;


import com.liang.blog.common.result.Result;
import com.liang.blog.service.BlogFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author：YanWenLiang
 * @Date:2023-09-08-20:31
 */
@RestController
@CrossOrigin
public class BlogFileController {
    @Autowired
    private BlogFileService blogFileService;

    @PostMapping("fileUpload")
    public Result fileUpLoad(MultipartFile file){
        String path = blogFileService.uploadAvatar(file);
        return Result.ok(path);
    }

    @DeleteMapping("deleteAvatar/{fileName}")
    public Result deleteAvatar(@PathVariable String fileName){
        blogFileService.deleteAvatar(fileName);
        return Result.ok();
    }

    /**
     * 上传接口
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/local/upload")
    public Result upload(MultipartFile file) {
        String upload = blogFileService.uploadLocal(file, "avatar");
        return Result.ok(upload);
    }

    /**
     * 上传接口
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/local/uploadMsgFile")
    public Result uploadMsgFile(MultipartFile file) {
        String upload = blogFileService.uploadLocal(file, "msg");
        return Result.ok(upload);
    }

    /**
     * 上传接口
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/local/uploadArticleFile")
    public Result uploadArticleFile(@RequestParam("article") MultipartFile file) {
        String upload = blogFileService.uploadLocal(file, "article");
        return Result.ok(upload);
    }

    /**
     * 下载接口
     * @param flag
     * @param response
     */
    @GetMapping("{folder}/{flag}")
    public void getFiles(@PathVariable String folder, @PathVariable String flag, HttpServletResponse response) {
       blogFileService.getFiles(folder, flag, response);
    }
}
